package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzRequestParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    private ClazzService clazzService;

    /**
     * 条件分页查询班级列表
     * */
    @GetMapping
    public Result page(ClazzRequestParam clazzRequestParam){
        //开启日志记录
        log.info("条件分页查询到的班级请求参数：{}", clazzRequestParam);

        //调用service条件分页查询
        PageResult<Clazz> pageResult = clazzService.page(clazzRequestParam);

        //将PageResult封装成Result对象并返回
        return Result.success(pageResult);
    }

    /**
     * 删除班级
     * */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        //开启日志记录
        log.info("删除班级，{}", id);

        //调用Service层删除指定班级
        clazzService.deleteById(id);

        //返回成功结果
        return Result.success();
    }

    /**
     * 添加班级
     * */
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        //开启日志记录
        log.info("添加班级，{}", clazz);

        //调用Service层添加班级
        clazzService.add(clazz);

        //返回成功结果
        return Result.success();
    }

    /**
     * 根据id查询班级
     * */
    @GetMapping("/{id}")
    public Result select(@PathVariable Integer id){
        //开启日志记录
        log.info("根据id查询班级，{}", id);

        //调用Service层根据id查询班级
        Clazz clazz = clazzService.selectById(id);

        //将查询结果封装成Result对象并返回
        return Result.success(clazz);
    }

    /**
     * 修改班级
     * */
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        //开启日志记录
        log.info("修改后的班级信息，{}", clazz);

        //调用Service层修改班级
        clazzService.update(clazz);

        //返回成功结果
        return Result.success();
    }

    /**
     * 查询所有班级
     * */
    @GetMapping("/list")
    public Result selectAll(){
        return Result.success(clazzService.selectAll());
    }
}
